var Wow = {

	/**
	 * Display or hide the video.
	 */
	toggleInterceptVideo: function() {
		$("#video, #blackout, #play-trailer").toggle();
		return false;
	},

	/**
	 * Bind item tooltips to links.
	 * Gathers the item ID from the href, and the optional params from the data-item attribute.
	 */
	bindItemTooltips: function() {
		var doc = $(document);
		var callback = function() {
			if (this.href == 'javascript:;' || this.href.indexOf('#') >= 0)
				return;
			
			var id = null,
				ie = Core.isIE(),
				query = "";

			if (this.href && this.rel != 'np' && this.href.indexOf('http://eu.battle.net/item/') >= 0) {
				var href = this.getAttribute('href');

				if (ie)
					href = href.replace(location.protocol +'//'+ location.host, "");

				id = parseInt(href.replace(Core.baseUrl +'/item/', ""));
			}

			if (id) {
				if (dataItem = this.getAttribute('data-item'))
					query = '?'+ dataItem;

				Tooltip.show(this, '/item/'+ id +'/tooltip'+ query, true);
			}
		}

		doc.undelegate('a', 'mouseover.tooltip', callback);
		doc.delegate('a', 'mouseover.tooltip', callback);
	},

	/**
	 * Update the events within the sidebar.
	 *
	 * @param int id
	 * @param string status
	 */
	updateEvent: function(id, status) {
		alert('@fix - All event AJAX calls fail');
		return;
		
		$('#event-'+ id +' .actions').fadeOut('fast');

		$.ajax({
			url: $('.card-overlay').attr('href') +'event/'+ status,
			data: { eventId: id },
			dataType: "json",
			success: function(data) {
				$('#event-'+ id).fadeOut('fast', function() {
					$(this).remove();
				});
			}
		});

		return false;
	},

	createSimpleTooltip: function(title, description, icon) {

		var $tooltip = $('<ul/>');

		// Icon
		if(icon) {
			$('<li/>').append(Wow.Icon.framedIcon(icon, 56)).appendTo($tooltip);
		}

		// Title
		if(title) {
			$('<li/>').append($('<h3/>').text(title)).appendTo($tooltip);
		}

		// Description
		if(description) {
			$('<li/>').addClass('color-tooltip-yellow').text(description).appendTo($tooltip);
		}

		return $tooltip;
	}

};

Wow.Icon = {

	/**
	 * CDN for images.
	 */
	cdn: 'http://us.battle.net/wow-assets/static/images',

	/**
	 * Generate icon path.
	 *
	 * @param string name
	 * @param int size
	 */
	getUrl: function(name, size) {
		return Wow.Icon.cdn +'/icons/'+ size +'/'+ name +'.jpg';
	},

	/**
	 * Create frame icon markup.
	 *
	 * @param string name
	 * @param int size
	 */
	framedIcon: function(name, size) {
		var iconSize = 56;

		if(size <= 18) {
			iconSize = 18;
		} else if(size <= 36) {
			iconSize = 36;
		}

		var $icon = $('<span/>').addClass('icon-frame frame-' + size);

		if(size == 18 || size == 36 || size == 56) {
			$icon.css('background-image', 'url(' + Wow.Icon.getUrl(name, iconSize) + ')');
		} else {
			$icon.append($('<img/>').attr({
				width: size,
				height: size,
				src: Wow.Icon.getUrl(name, iconSize)
			}));
		}

		return $icon;
	}

}

$(document).ready(function() {
	Wow.bindItemTooltips();
});